<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <!-- <a-col :span="12">
          <a-form-item label="汇报人" v-bind="validateInfos.reportUser">
            <a-input v-model:value="formData.reportUser" placeholder="请输入汇报人" disabled allowClear></a-input>
          </a-form-item>
        </a-col> -->
        <a-col :span="12">
          <a-form-item label="所属项目" v-bind="validateInfos.projectId">
	          <!-- <j-search-select v-model:value="formData.projectId" dict="tm_project,project_name,id" @change="projectChange" :disabled="disabled" allowClear/> -->
            <j-select-multiple type="list_multi" v-model:value="formData.projectId" dictCode="tm_project,project_name,id" @change="projectChange" placeholder="请选择项目" :disabled="disabled" :triggerChange="false" allowClear/>

          </a-form-item>
        </a-col>
        <!-- <a-col :span="6">
          <a-form-item label="所属系统" v-bind="validateInfos.systemName">
	          <j-search-select ref="refSystem" v-model:value="formData.systemName" :dict="searchSystemName"  :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col> -->
        <!-- <a-col :span="12">
          <a-form-item label="项目批次" v-bind="validateInfos.projectBatch">
	          <j-search-select v-model:value="formData.projectBatch" dict="tm_project_batch,batch_name,id" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col> -->
        <a-col :span="12">
          <a-form-item label="是否上线" v-bind="validateInfos.online">
	          <j-dict-select-tag v-model:value="formData.online" dictCode="have_or_no" placeholder="请选择是否上线" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="上线问题" v-bind="validateInfos.onlineIssue">
	          <j-dict-select-tag v-model:value="formData.onlineIssue" dictCode="have_or_no" placeholder="请选择上线问题" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="项目经理">
	          <j-search-select ref="refPm" v-model:value="formData.pm" :dict="searchPm" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="编写用例数" v-bind="validateInfos.writeCaseNum">
	          <a-input-number v-model:value="formData.writeCaseNum" placeholder="请输入编写用例数" style="width: 100%" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="执行用例数" v-bind="validateInfos.executeCaseNum">
	          <a-input-number v-model:value="formData.executeCaseNum" placeholder="请输入执行用例数" style="width: 100%" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="提交缺陷数" v-bind="validateInfos.defectBugNum">
	          <a-input-number v-model:value="formData.defectBugNum" placeholder="请输入提交缺陷数" style="width: 100%" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="参会数" v-bind="validateInfos.attendMeeting">
	          <a-input-number v-model:value="formData.attendMeeting" placeholder="请输入参会数" style="width: 100%" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="本周工作" v-bind="validateInfos.weekJob">
	          <a-textarea v-model:value="formData.weekJob" :rows="4" placeholder="请输入本周工作" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="下周工作" v-bind="validateInfos.nextWeekJob">
	          <a-textarea v-model:value="formData.nextWeekJob" :rows="4" placeholder="请输入下周工作" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="项目风险" v-bind="validateInfos.projectRisk">
	          <a-textarea v-model:value="formData.projectRisk" :rows="4" placeholder="请输入项目风险" :disabled="disabled" allowClear/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../WeekReport.api';
  import { Form } from 'ant-design-vue';
  import {useUserStore} from "@/hooks/useUserStore";
  import JSelectMultiple from '/@/components/Form/src/jeecg/components/JSelectMultiple.vue';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';


  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });

  const searchSystemName = ref<string>("");
  const searchPm = ref<string>("");
  const refSystem = ref();
  const refPm = ref();
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    reportUser: '',   
    projectId: '',   
    systemName: '',   
    projectBatch: '',   
    online: '',
    onlineIssue: '',
    pm: '',   
    writeCaseNum: undefined,
    executeCaseNum: undefined,
    defectBugNum: undefined,
    attendMeeting: undefined,
    weekJob: '',   
    nextWeekJob: '',   
    projectRisk: '',   
  });
  const { createMessage } = useMessage();
  // const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  // const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const labelCol = ref<any>({style: { width: '100px' }})
  const wrapperCol = ref<any>({});

  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    projectId: [{ required: true, message: '请输入项目!'},],
    systemName: [{ required: true, message: '请输入系统!'},],
    // projectBatch: [{ required: true, message: '请输入项目批次!'},],
    // pm: [{ required: true, message: '请输入项目经理!'},],
    online: [{ required: true, message: '请选择是否上线!'},],
    onlineIssue: [{ required: true, message: '请选择上线问题!'},],
    writeCaseNum: [{ required: true, message: '请输入编写用例数!'},],
    executeCaseNum: [{ required: true, message: '请输入执行用例数!'},],
    defectBugNum: [{ required: true, message: '请输入提交缺陷数!'},],
    attendMeeting: [{ required: true, message: '请输入参会数!'},],
    weekJob: [{ required: true, message: '请输入本周工作!'},],
    nextWeekJob: [{ required: true, message: '请输入下周工作!'},],
    projectRisk: [{ required: true, message: '请输入项目风险!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  function projectChange(item) {
    console.log('projectChange item:' + item)
    formData.projectId=item
    searchSystemName.value = "tm_project_system where project_id='"+item+"',system_name,id"
    refSystem.value.initSelectValue();
    searchPm.value = "tm_project_user where project_id='"+item+"',realname,user_id"
    refPm.value.initSelectValue();
    formData.systemName=''

    // refSystem.value.loading()
  }
  
  /**
   * 新增
   */
  function add() {
    // formData.reportUser = userInfo.username
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      searchSystemName.value = "tm_project_system where project_id='"+record.projectId+"',system_name,id"
      refSystem.value.initSelectValue();
      searchPm.value = "tm_project_user where project_id='"+record.projectId+"',realname,user_id"
     refPm.value.initSelectValue();
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 800px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
